<?php

require( 'util.php' );
validate_action_get( array( 'mid' ) );

$mid = $_GET['mid'];
$page = isset( $_GET['page'] ) ? $_GET['page'] : 1;

require( 'db.php' );
$model = get_model( $mid );

$numThreadsPerPage = 5; // 每页显示多少个帖子
$numPages = 0;

calculate_pages();

?>

<?php

function calculate_pages()
{
    global $mid, $page, $numPages, $numThreadsPerPage;
    $numThreads = 0;
    
    $sql = "SELECT COUNT(*) FROM `page` WHERE m_id = $mid";
    $result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
    
    if ($result && mysql_num_rows( $result ) )
    {
        $arr = mysql_fetch_array( $result );
        $numThreads = $arr[0];
    }
    
    $numPages = (int) ($numThreads / $numThreadsPerPage);
    if ($numThreads % $numThreadsPerPage)
        $numPages++;
    
    if ($page < 1 || $page > $numPages)
        report_err_and_exit( '无效页码。' );
}

// 页码控制
function output_page_list()
{
    global $mid, $page, $numPages;
    
    echo <<<ROD
<div id="pgt" class="bm bw0 pgs cl" style="margin-top:10px">
    <div class="pg">
ROD;

    for ($i = 1; $i <= $numPages; $i++)
    {
        if ($i == $page)
        {
            echo "<strong>$i</strong>";
        }
        else
        {
            echo "<a href='list.php?mid=$mid&page=$i'>$i</a>";
        }
    }
    
    if ($page < $numPages)
    {
        $next = $page + 1;
        echo "<a href='list.php?mid=$mid&page=$next' class='nxt'>下一页</a>";
    }
    
    echo <<<ROD
    </div>
    <span class="pgb y" id="visitedforums"><a href="#">返回</a></span>
    <a href="new_page.php?mid=$mid&mode=write" id="newspecial" title="发新帖">
        <img src="static/image/common/pn_post.png" alt="发新帖">
    </a>
</div>
ROD;
}

function output_thread_list()
{
    global $mid, $page, $numThreadsPerPage;
    
    $from = ($page - 1) * $numThreadsPerPage;
    $to = $from + $numThreadsPerPage;

    $sql = "SELECT p_id, user.name as uname, subject, dateline, last_replied, comment_times, access_times FROM `page`, `user` WHERE m_id = $mid AND user.u_id = page.u_id ORDER BY last_replied DESC LIMIT $from, $to";
    $result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );

    if ($result)
    {
        while ($thread = mysql_fetch_array( $result ))
        {
            output_thread( $thread );
        }
    }
    else
    {
        echo '没有帖子！';
    }
}

function output_thread($thread)
{
    $dateline = date( 'Y-m-d', strtotime( $thread['dateline'] ) );
    $url = 'view_page.php?pid=' . $thread['p_id'];

    echo <<<ROD
<tbody>
    <tr>
        <td class="icn">
            <a href="$url" title="新窗口打开" target="_blank">
                <img src="static/image/common/folder_common.gif" alt="新窗口打开">
            </a>
        </td>

        <td class="common"><a href="$url" class="xst">{$thread['subject']}</a></td>

        <td class="by">
            <cite>{$thread['uname']}</cite>
            <em><span>$dateline</span></em>
        </td>

        <td class="num">
            <a href="$url" class="xi2">{$thread['comment_times']}</a>
            <em>{$thread['access_times']}</em>
        </td>

        <td class="by">{$thread['last_replied']}</td>
    </tr>
</tbody>
ROD;
}

//********************** 页面内容开始 **********************
?>

<?php

require_once( 'page_frame.inc.php' );

output_frame_header( "帖子列表 - 第 $page 页 - {$model['topic']} - {$model['message']}" );

?>

<?php output_page_list(); ?>

<div id="threadlist" class="tl bm bmw">
    <div class="th">
        <table cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <th colspan="2"><div class="tf"></div></th>
                    <td class="by">作者</td>
                    <td class="num">回复/查看</td>
                    <td class="by">最后发表</td>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="bm_c">
        <div id="forumnew" style="display:none"></div>
        <table summary="forum_4" cellspacing="0" cellpadding="0">

            <?php output_thread_list(); ?>

        </table>
    </div>
</div>

<?php output_page_list(); ?>

<?php

//********************** 页面内容结束 **********************

output_frame_footer();

?>